package main

import (
	"fmt"
	"net"
)

import "packet"

func recvUDPMsg(conn *net.UDPConn) {
	var buf [4096]byte

	n, raddr, err := conn.ReadFromUDP(buf[0:])
	packet.CheckError(err)

	pkt_recv, err := packet.ConvertToPacket(buf[0:n])
	packet.CheckError(err)

	fmt.Println(pkt_recv)

	pkt_send := packet.NPacket{Version: pkt_recv.Version, ID: pkt_recv.ID}
	copy(pkt_send.Buffer[:], "Pong")

	send_buff, err := packet.ConvertToBuffer(&pkt_send)
	packet.CheckError(err)
	_, err = conn.WriteToUDP(send_buff, raddr)
	packet.CheckError(err)
}

func main() {
	udp_addr, err := net.ResolveUDPAddr("udp", ":11110")
	packet.CheckError(err)

	conn, err := net.ListenUDP("udp", udp_addr)
	defer conn.Close()
	packet.CheckError(err)

	recvUDPMsg(conn)
}
